<template>
	<view class="viewBg">
		<!-- <mSearch :mode="2" button="outside" radius="5" @search="search($searchKey,2)"></mSearch> -->

		<view class="uni-list">
			<view class="uni-list-cell" v-for="(item,index) in list" :key="index" @click="goDetail(item)">
				<view class="uni-list-cell-navigate uni-navigate-right">
					{{item.title}}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import mSearch from "@/components/mehaotian-search/mehaotian-search.vue";

	export default {
		components: {
			mSearch
		},
		data() {
			return {
				list: [],
				searchKey: '',
				current: 0,
				activeColor: '#007aff',
				styleType: 'button',

				reload: false,

				pageNo: 1,
				viewStatus: 0,
				savePath: ''
			};
		},
		onLoad() {
			this.getList();
		},
		onReachBottom() {
			this.pageNo++;
			this.getList();
		},
		methods: {
			getList() {
				this.reload = false;
				uni.stopPullDownRefresh();

				this.$http.get('ac/xagrid/appNewsService/getNewsList', {
					pageNo: this.pageNo,
					pageSize: 10,
					viewStatus: this.viewStatus
				}).then((res) => {
					if (res.errcode === '0') {
						const obj = res.data.results;
						if (obj.length > 0) {
							this.list = this.list.concat(obj);
						}
					} else {
						console.log("错误信息" + res.errmsg);
						uni.showToast({
							title: res.errmsg,
							mask: false,
							duration: 1500
						});
					}
				});
			},
			goDetail(e) {
				// 				uni.navigateTo({
				// 					url: '../lawDetail/lawDetail?id=' + e.id,
				// 				});
				this.$http.get('ac/xagrid/appNewsService/getNewsDetail', {
					id: e.id,
				}).then((res) => {
					if (res.errcode === '0') {
						let internetFile = res.data.content;
						let index = internetFile.lastIndexOf("/"); //若不能识别，前面加转义字符 \
						let localFile = internetFile.substring(index + 1, internetFile.length);
						console.log(localFile);
						
						uni.getStorage({
							key: localFile,
							success: function(res) {
								console.log(res.data);
								uni.openDocument({
									filePath: res.data,
									success: () => {
										plus.nativeUI.toast('打开本地文件成功');
									},
									fail: () => {
										plus.nativeUI.toast('未安装内置应用');
									}
								});
							},
							fail: () => {
								uni.downloadFile({
									url: 'http://image.tripln.top' + res.data.content,
									success: (res) => {
										uni.saveFile({
											tempFilePath: res.tempFilePath,
											success: function(res) {
												var savedFilePath = res.savedFilePath;
												uni.setStorage({
													key: localFile,
													data: savedFilePath,
													success: function() {
														console.log('success');
													}
												});
												uni.openDocument({
													filePath: savedFilePath,
													success: (res) => {
														plus.nativeUI.toast('打开成功');
													},
													fail: () => {
														plus.nativeUI.toast('未安装内置应用');
													}
												});
											}
										});
									}
								});
							}
						});
					} else {
						uni.showToast({
							title: res.errmsg,
							icon: 'none',
							duration: 1500
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.viewBg {
		width: 100%;
		height: 100%;
		/* background-color: #0FAEFF; */
	}

	.aaa {
		width: 100upx;
		height: 100upx;
		background-color: #007AFF;
	}
</style>
